package com.baosight.sczx.zm.ch.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cg.DailyReport;
import com.baosight.sczx.common.zm.cInterface.ch.WorkGroup;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ch.domain.ZMCHHoleReport;

/**
 * <p>Title: ServiceZMCH20.java</p>
 * <p>Description: 抽采进度日汇报</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-10-25 下午12:14:15  张小飞  Create
 *</pre>
 */
public class ServiceZMCH20 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCH20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:26:45 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCH20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:26:59 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		
		String fdMiningId = (String)UserSession.getInSessionProperty("miningCode");
	    String fdDepartmentId = (String)UserSession.getInSessionProperty("orgCode"); 

	    if(fdDepartmentId != null && fdDepartmentId.length() > 9) {
	    	fdDepartmentId = fdDepartmentId.substring(0, 9);
	    }
	    
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		StaticTools.setQueryParameters(inInfo, "fdReportDepartmentId", fdDepartmentId, true);

		EiInfo outInfo = super.query(inInfo, "ZMCHHoleReport.query", new ZMCHHoleReport());

		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.ch.workNum", fdMiningId, true));
		outInfo.addBlock(WorkGroup.getWorkGroupBlock("cmes.ch.workGroup", fdMiningId, fdDepartmentId, "抽采", true));
		
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[]{"草拟", "上报"}, new String[]{"草拟", "上报"}, true));//状态
		return outInfo;
	}	
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCH20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:31:06 张小飞
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		
		EiInfo out=new EiInfo(); 
		ZMCHHoleReport _dom = new ZMCHHoleReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
	
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_HOLEREPORT"));
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
			_dom.setFdReportDepartmentId(UserSession.getInSessionProperty("orgCode").toString());// 汇报部门id
			_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());// 汇报部门
			_dom.setFdReporterId(UserSession.getUserId().toString());// 汇报人ID
			_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());// 汇报人
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
			
			//校验是否重复
			boolean validate = this.validate(inInfo, _dom, "ZMCHHoleReport.validate", null);
			if(validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			this.dao.insert("ZMCHHoleReport.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCH20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午04:59:23 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {		
		EiInfo out=new EiInfo(); 
		ZMCHHoleReport _dom = new ZMCHHoleReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			// 校验是否重复
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = this.validate(inInfo, _dom, "ZMCHHoleReport.validate", params);
			if (validate) {
				out = this.query(inInfo);
				return out;
			}
			_dom.setFdReporterId(UserSession.getUserId().toString());// 汇报人ID
			_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());// 汇报人
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(), _dom.getFdWorknumId()));
			this.dao.update("ZMCHHoleReport.update", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功"); 
		return out; 
	}	
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCH20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午04:12:10 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCHHoleReport.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}	
	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-22上午10:59:40 张小飞
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCHHoleReport _dom = new ZMCHHoleReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			Map<String, String> map = new HashMap<String, String>();
			map.put("fdCurrentDate", _dom.getFdCurrentDate());
			map.put("fdReportDepartmentId", _dom.getFdReportDepartmentId());
			map.put("fdWorknumId", _dom.getFdWorknumId());
			map.put("fdProjectId", _dom.getFdProjectId());
			
			boolean validate = DailyReport.validate("ZMCGHoleReportFinal.query", map);
			if(validate) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("已经存在数据，无需上报！");
				return out; 
			}
			else {
			    int result = this.dao.update("ZMCHHoleReport.report", _dom);
			    if(result > 0){
					if(_dom.getFdReportDepartmentId() != null) {
						String fdReportDepartmentId = _dom.getFdReportDepartmentId().substring(0, 9);
						_dom.setFdReportDepartmentId(fdReportDepartmentId);
						_dom.setFdReportDepartment(Department.getInstance().getDepartmentNameById(fdReportDepartmentId));
					}

			    	DailyReport.copy("ZMCGHoleReportFinal.insert", _dom.toMap());
			    }
			    }
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
	
	/**
	 * <p>Function: validate</p>
	 * <p>Description: 对主表记录新增，删除，上报前的后台校验</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:40:23 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCHHoleReport _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}

		params.put("fdWorknumId", _dom.getFdWorknumId());
		params.put("fdMiningId", _dom.getFdMiningId());
		params.put("fdCurrentDate", _dom.getFdCurrentDate());
		params.put("fdReportDepartmentId", _dom.getFdReportDepartmentId());
		params.put("fdProjectId", _dom.getFdProjectId());
		
		List result = dao.query("ZMCHHoleReport.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "抽采进度日汇报每矿每班每天只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
	
}
